package com.tebiecloud.act.server.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.tebiecloud.act.client.model.entity.Act;
import com.tebiecloud.act.client.model.entity.ActGiftPack;
import com.tebiecloud.act.client.model.entity.ActGoodsRef;
import com.tebiecloud.act.client.model.vo.ActListVO;
import com.tebiecloud.act.server.service.ActGiftPackService;
import com.tebiecloud.act.server.service.ActGoodsRefService;
import com.tebiecloud.act.server.service.ActService;
import com.tebiecloud.act.server.service.feign.GoodsServiceClient;
import com.tebiecloud.act.server.service.feign.StoreServiceClient;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.goods.client.model.entity.Goods;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import com.tebiecloud.goods.client.model.vo.GoodsListVO;
import com.tebiecloud.goods.client.model.vo.SpecificationList;
import com.tebiecloud.store.client.model.entity.Store;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 活动表
 前端控制器
 * @author tebie
 * @date 2019-09-12
 */
@Slf4j
@RestController
@RequestMapping("/act")
public class ActController {

    @Autowired
    private ActService actService;

    @Autowired
    private ActGoodsRefService actGoodsRefService;

    @Autowired
    private StoreServiceClient storeServiceClient;

    @Autowired
    private GoodsServiceClient goodsServiceClient;

    @Autowired
    private ActGiftPackService actGiftPackService;

    /**
     * 首页获取9块9活动列表
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody<List<GoodsListVO>> list(
            @RequestParam(value = "storeId", required = false) Long storeId) {
        if(storeId==null){
            //查询随意一家门店
            ResultBody<List<Store>> storeResultBody = storeServiceClient.list();
            if(storeResultBody!=null && storeResultBody.getCode() == ResultEnum.OK.getCode()){
                List<Store> storeList = storeResultBody.getData();
                if(storeList.size()>0){
                    storeId = storeList.get(0).getStoreId();
                }else{
                    log.info(">>minjie,没有查询到门店");
                    return ResultBodyUtils.failed("没有查询到门店");
                }
            }
        }
        return ResultBodyUtils.ok(actService.list(storeId));
    }

    /**
     * 新增活动
     * @param name 活动名称,
     * @param actType 活动类型：1爆品活动，2礼包活动,
     * @param mainPic 主图,
     * @param shelfTime 上架时间,
     * @param startTime 开售时间,
     * @param endTime 结束时间,
     * @param codeStime 有效开始时间,
     * @param codeEtime 有效结束时间,
     * @param carouselPics 轮播图,
     * @param detailPics 详情图,
     * @param sharePic 分享图片,
     * @param shareTitle 分享标题,
     * @param shareRemark 分享描述,
     * @param status 启用状态：0、停用，1、启用,
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "actType", required = false) Integer actType,
            @RequestParam(value = "mainPic", required = false) String mainPic,
            @RequestParam(value = "shelfTime", required = false) String shelfTime,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "codeStime", required = false) String codeStime,
            @RequestParam(value = "codeEtime", required = false) String codeEtime,
            @RequestParam(value = "effectiveDay", required = false) Integer effectiveDay,
            @RequestParam(value = "effectiveType", required = false,defaultValue = "2") Integer effectiveType,
            @RequestParam(value = "isRefundable", required = false,defaultValue = "1") int isRefundable,
            @RequestParam(value = "purchaseQty", required = false,defaultValue = "-1") Integer purchaseQty,
            @RequestParam(value = "carouselPics", required = false) String carouselPics,
            @RequestParam(value = "detailPics", required = false) String detailPics,
            @RequestParam(value = "sharePic", required = false) String sharePic,
            @RequestParam(value = "shareTitle", required = false) String shareTitle,
            @RequestParam(value = "shareRemark", required = false) String shareRemark,
            @RequestParam(value = "des", required = false) String des,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "price", required = false) Integer price,
            @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
            @RequestParam(value = "commissionRate", required = false) Integer commissionRate,
            @RequestParam(value = "stockQty", required = false,defaultValue = "-99") Integer stockQty,
            @RequestParam(value = "specificationlist", required = false) String specificationlist) {
        Act act = new Act();
        act.setStoreId(storeId);
        act.setName(name);
        act.setActType(actType);
        act.setMainPic(mainPic);
        act.setShelfTime(DateUtils.parseDate(shelfTime));
        act.setStartTime(DateUtils.parseDate(startTime));
        act.setEndTime(DateUtils.parseDate(endTime));
        act.setCodeStime(DateUtils.parseDate(codeStime));
        act.setCodeEtime(DateUtils.parseDate(codeEtime));
        act.setEffectiveDay(effectiveDay);
        act.setEffectiveType(effectiveType);
        act.setIsRefundable(isRefundable);
        act.setPurchaseQty(purchaseQty);
        act.setCarouselPics(carouselPics);
        act.setDetailPics(detailPics);
        act.setSharePic(sharePic);
        act.setShareTitle(shareTitle);
        act.setShareRemark(shareRemark);
        act.setDes(des);
        act.setStatus(status);
        if(actService.save(act)){
            if(act.getActType().equals(2)){//添加体验活动价格
                ActGiftPack actGiftPack = new ActGiftPack();
                actGiftPack.setActivityId(act.getActId());
                actGiftPack.setCommissionRate(commissionRate);
                actGiftPack.setCostPrice(retailPrice);
                actGiftPack.setPrice(price);
                actGiftPack.setRetailPrice(retailPrice);
                actGiftPack.setStockQty(stockQty);
                actGiftPackService.save(actGiftPack);
                JSONArray jsonArray = JSONArray.parseArray(specificationlist);
                List<SpecificationList> specificationLists = jsonArray.toJavaList(SpecificationList.class);
                List<ActGoodsRef> actGoodsRefList = Lists.newLinkedList();
                for(SpecificationList specificationList:specificationLists){
                    ActGoodsRef actGoodsRef = new ActGoodsRef();
                    actGoodsRef.setStockQty(actGiftPack.getStockQty());
                    actGoodsRef.setActId(act.getActId());
                    actGoodsRef.setCommissionRate(actGiftPack.getCommissionRate());
                    actGoodsRef.setCostPrice(actGiftPack.getRetailPrice());
                    actGoodsRef.setGoodsId(specificationList.getGoodsId());
                    actGoodsRef.setPrice(actGiftPack.getPrice());
                    actGoodsRef.setRetailPrice(actGiftPack.getRetailPrice());
                    actGoodsRef.setSpecId(specificationList.getSpecId());
                    actGoodsRef.setDeleted(0);
                    actGoodsRefList.add(actGoodsRef);
                }
                actGoodsRefService.saveBatch(actGoodsRefList);
            }else if(StringUtils.isNotEmpty(specificationlist)){
                JSONArray jsonArray = JSONArray.parseArray(specificationlist);
                List<SpecificationList> specificationLists = jsonArray.toJavaList(SpecificationList.class);
                List<ActGoodsRef> actGoodsRefList = Lists.newLinkedList();
                for(SpecificationList specificationList:specificationLists){
                    ActGoodsRef actGoodsRef = new ActGoodsRef();
                    actGoodsRef.setStockQty(specificationList.getStockQty());
                    actGoodsRef.setActId(act.getActId());
                    actGoodsRef.setCommissionRate(specificationList.getCommissionRate());
                    actGoodsRef.setCostPrice(specificationList.getRetailPrice());
                    actGoodsRef.setGoodsId(specificationList.getGoodsId());
                    actGoodsRef.setPrice(specificationList.getPrice());
                    actGoodsRef.setRetailPrice(specificationList.getRetailPrice());
                    actGoodsRef.setSpecId(specificationList.getSpecId());
                    actGoodsRef.setDeleted(0);
                    actGoodsRefList.add(actGoodsRef);
                }
                actGoodsRefService.saveBatch(actGoodsRefList);
            }
        }
        return ResultBodyUtils.ok();
    }


//    /**
//     * 活动详情接口
//     * @param actId //活动id
//     * @param lng //经度
//     * @param lat //纬度
//     * @return
//     */
//    @PostMapping("info")
//    public ResultBody<Act> info(
//            @RequestParam("actId") Long actId,
//            @RequestParam(value = "lng", required = false) Double lng,
//            @RequestParam(value = "lat", required = false) Double lat) {
//        return ResultBodyUtils.ok(actService.getActVO(actId,lng,lat));
//    }
//
//    /**
//     * 根据ID查找数据
//     * @param id
//     * @return
//     */
//    @GetMapping("get")
//    public ResultBody<Act> get(@RequestParam("id") Long id) {
//        Act entity = actService.getById(id);
//        return ResultBodyUtils.ok(entity);
//    }
//

//
//    /**
//     * 更新数据
//     * @param name 活动名称
//     * @param actType 活动类型：1爆品活动，2礼包活动
//     * @param mainPic 主图
//     * @param shelfTime 上架时间
//     * @param startTime 开售时间
//     * @param endTime 结束时间
//     * @param codeStime 有效开始时间
//     * @param codeEtime 有效结束时间
//     * @param carouselPics 轮播图
//     * @param detailPics 详情图
//     * @param sharePic 分享图片
//     * @param shareTitle 分享标题
//     * @param shareRemark 分享描述
//     * @param status 启用状态：0、停用，1、启用
//     * @param deleted 删除状态：0、未删除，1、已删除
//     * @return
//     */
//    @PostMapping("update")
//    public ResultBody update(
//            @RequestParam(value = "actId", required = false) Long actId,
//            @RequestParam(value = "name", required = false) String name,
//            @RequestParam(value = "actType", required = false) Integer actType,
//            @RequestParam(value = "mainPic", required = false) String mainPic,
//            @RequestParam(value = "shelfTime", required = false) Date shelfTime,
//            @RequestParam(value = "startTime", required = false) Date startTime,
//            @RequestParam(value = "endTime", required = false) Date endTime,
//            @RequestParam(value = "codeStime", required = false) Date codeStime,
//            @RequestParam(value = "codeEtime", required = false) Date codeEtime,
//            @RequestParam(value = "carouselPics", required = false) String carouselPics,
//            @RequestParam(value = "detailPics", required = false) String detailPics,
//            @RequestParam(value = "sharePic", required = false) String sharePic,
//            @RequestParam(value = "shareTitle", required = false) String shareTitle,
//            @RequestParam(value = "shareRemark", required = false) String shareRemark,
//            @RequestParam(value = "status", required = false) Integer status,
//            @RequestParam(value = "deleted", required = false) Integer deleted
//    ) {
//        Act entity = new Act();
//        entity.setActId(actId);
//        entity.setName(name);
//        entity.setActType(actType);
//        entity.setMainPic(mainPic);
//        entity.setShelfTime(shelfTime);
//        entity.setStartTime(startTime);
//        entity.setEndTime(endTime);
//        entity.setCodeStime(codeStime);
//        entity.setCodeEtime(codeEtime);
//        entity.setCarouselPics(carouselPics);
//        entity.setDetailPics(detailPics);
//        entity.setSharePic(sharePic);
//        entity.setShareTitle(shareTitle);
//        entity.setShareRemark(shareRemark);
//        entity.setStatus(status);
//        entity.setDeleted(deleted);
//        actService.updateById(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 删除数据
//     * @param id 单个id
//     * @param ids 多个id用,号隔开
//     * @return
//     */
//    @PostMapping("remove")
//    public ResultBody remove(
//            @RequestParam(value = "id", required = false) Long id,
//            @RequestParam(value = "ids", required = false) String ids
//    ) {
//        if(id != null && id > 0) {
//            actService.removeById(id);
//        }
//        if(ids != null && ids.length() > 0) {
//            actService.removeByIds(Arrays.asList(ids.split(",")));
//        }
//        return ResultBodyUtils.ok();
//    }

}
